package NetRequest;

import android.content.Context;
import android.util.Log;

import com.example.dell.avtivity.URL;

import java.net.URLEncoder;
import java.util.LinkedHashMap;

import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;

/**
 * Created by dell on 2017/3/5.
 */

public class SendRuest {

    public SendRuest(Context context) {
        context.getMainLooper();
    }

    public SendRuest() {
    }

    private static final String TAG = "TAG";

    /*
    注册请求
     */
    public static void SendRuest_Register(String tel, String password, okhttp3.Callback callback) {

        try {
            OkHttpClient client = new OkHttpClient();
            RequestBody requestBody = new FormBody.Builder()
                    .add("phone", tel)
                    .add("password", password)
                    .build();
            Request request = new Request.Builder()
                    .url(com.example.dell.avtivity.URL.Register)
                    .post(requestBody)
                    .build();
            client.newCall(request).enqueue(callback);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /*
    手机快捷登录请求
     */
    public static void SendRuest_LoginWithPhone(String tel, okhttp3.Callback callback) {

        try {
            OkHttpClient client = new OkHttpClient();
            RequestBody requestBody = new FormBody.Builder()
                    .add("phone", tel)
                    .build();
            Request request = new Request.Builder()
                    .url(URL.LoginWithPhone)
                    .post(requestBody)
                    .build();
            client.newCall(request).enqueue(callback);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /*
   账号登录
    */
    public static void SendRequest_LoginWithUserName(String input, String password, okhttp3.Callback callback) {
        try {
            OkHttpClient client = new OkHttpClient();
            RequestBody requestBody = new FormBody.Builder()
                    .add("input", input)
                    .add("password", password)
                    .build();
            Request request = new Request.Builder()
                    .url(com.example.dell.avtivity.URL.LoginWithUserManual)
                    .post(requestBody)
                    .build();
            client.newCall(request).enqueue(callback);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /*
    判断验证码是否正确
     */
    public static void isKeytrue(String phone, String key, okhttp3.Callback callback) {
        try {
            OkHttpClient client = new OkHttpClient();
            RequestBody requestBody = new FormBody.Builder()
                    .add("phone", phone)
                    .add("code", key)
                    .build();
            Request request = new Request.Builder()
                    .url(com.example.dell.avtivity.URL.isKeyTrue)
                    .post(requestBody)
                    .build();
            client.newCall(request).enqueue(callback);
            Log.d(TAG, "key running");
        } catch (Exception e) {
            e.printStackTrace();
            Log.d(TAG, "key error");
        }
    }

    /*
    注册时发送验证码
     */
    public static void SendKey_Register(String phone, okhttp3.Callback callback) {
        try {
            OkHttpClient client = new OkHttpClient();
            RequestBody requestBody = new FormBody.Builder()
                    .add("phone", phone)
                    .build();
            Request request = new Request.Builder()
                    .url(com.example.dell.avtivity.URL.GetKey_register)
                    .post(requestBody)
                    .build();
            client.newCall(request).enqueue(callback);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /*
    登录时发送验证码
     */
    public static void SendKey_Login(String phone, okhttp3.Callback callback) {
        try {
            OkHttpClient client = new OkHttpClient();
            RequestBody requestBody = new FormBody.Builder()
                    .add("phone", phone)
                    .build();
            Request request = new Request.Builder()
                    .url(com.example.dell.avtivity.URL.GetKey_phone)
                    .post(requestBody)
                    .build();
            client.newCall(request).enqueue(callback);
        } catch (Exception e) {
            e.printStackTrace();
            Log.d(TAG, "SendKey_Login error");
        }
    }


    /*
    显示热门搜索物品
    */
    public static void ShowHot(okhttp3.Callback callback) {

        try {
            OkHttpClient client = new OkHttpClient();
            Request request = new Request.Builder()
                    .url(URL.hot)
                    .build();
            client.newCall(request).enqueue(callback);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /*
    显示搜索结果
     */
    public static void Search_Result(String name, String pageNum, okhttp3.Callback callback) {
        StringBuilder temParams = new StringBuilder();
        try {
            StringBuilder sb = new StringBuilder();
            LinkedHashMap<String, String> map = new LinkedHashMap<String, String>();
            map.put("name", name);
            map.put("pageNum", pageNum);
            int pos = 0;
            for (String key : map.keySet()) {
                if (pos > 0) {
                    sb.append("&");
                }
                sb.append(String.format("%s=%s", key, URLEncoder.encode(map.get(key), "utf-8")));
                pos++;
            }
            String realURL = String.format("%s%s", URL.search_result, sb.toString());
            Log.d(TAG, "Search_Result:" + realURL);
            Log.d(TAG, "Search_Result1:" + sb.toString());
            OkHttpClient client = new OkHttpClient();
            Request request = new Request.Builder()
                    .url(realURL)
                    .build();
            client.newCall(request).enqueue(callback);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
